﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

namespace XLDL.Includes
{
       
    public partial class ChiTiet : System.Web.UI.UserControl
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            
            // Page.MaintainScrollPositionOnPostBack = true;
            if (!IsPostBack)
           {
               Session["slg"] = "";
               Session["size"] = "";
               Session["yes"] = "";
              try
               {
                   string ss = Request.QueryString["ss"].ToString();
                   Session["size"] = ss;
               }
               catch { }
                xuat_luoi();
               
           }
          
        }      

        protected void xuat_luoi()
        {

            try
            {
                string id = Request.QueryString["id"].ToString();
                string Nid = Request.QueryString["nid"].ToString();
                DataTable tb = new DataTable();
                tb = XLDL.XLDaTa("select * from V_SanPham where id_sp=" + int.Parse(id) + "");
                DataList_load.DataSource = tb;
                DataList_load.DataBind();
                //  Label lbl_img = new Label();
                StringBuilder str = new StringBuilder();
                if (tb.Rows[0]["moi_cu"].ToString() == "True")
                {
                    str.Append("<img src='Images/new_icon.gif' border='0'/>");
                }
                lbl_ten.Text = "<a href='index.aspx?act=sanpham&nid=" + tb.Rows[0]["ID_nhom_SP"].ToString() + "&Gt=" + tb.Rows[0]["id_gt"].ToString() + "'>" + tb.Rows[0]["ten_nhom_sp"].ToString() + "</a> > " + tb.Rows[0]["ten_sp"].ToString().Trim() + " (MS" + tb.Rows[0]["id_sp"].ToString() + ")" + str.ToString();
                GioHang gh = new GioHang();
                this.PreRender += new EventHandler(gh.GioHang_PreRender);
                load_hinh_anh(int.Parse(id));


                XL_DS_DaXem his = (XL_DS_DaXem)Session["his"];
                his.kiemtra(tb.Rows[0]["hinh_anh"].ToString());
                his.them(tb.Rows[0]["hinh_anh"].ToString(), Request.Url.ToString());
                Session["his"] = his;
            }
            catch { }
          
            
           
        }

       
        protected void Counter(int ID_SP)
        {
            if (Session["yes"].ToString() =="")
            {
                DataTable tb = new DataTable();
                string sql = "select * from counter where id_sp=" + ID_SP + "";
                tb = XLDL.XLDaTa(sql);
                //for (int j = 0; j <= tb.Rows.Count - 1; j++)
                //{
                if (tb.Rows.Count == 0)
                {
                    string lenh = "insert into Counter(id_sp,solanxem_sp,ngayxem_gannhat) values(" + ID_SP + ",1,'" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "')";
                    tb = new DataTable();
                    tb = XLDL.XLDaTa(lenh);
                }
                else
                {
                    int slx = int.Parse(tb.Rows[0]["solanxem_sp"].ToString()) + 1;
                    string lenh = "update counter set solanxem_sp=" + slx + ", ngayxem_gannhat='" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "' where id_sp=" + ID_SP + "";
                    tb = new DataTable();
                    tb = XLDL.XLDaTa(lenh);
                }
                Session["yes"] = "true";
               
            }
        }
        protected void DataList_load_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            // Size
            Label id = new Label();
            id = (Label)e.Item.FindControl("lbl_ID_SP"); // id_SP
            //list Size
            DropDownList drp = new DropDownList();
            drp = (DropDownList)e.Item.FindControl("drp_so_size");
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from V_Size where id_sp=" + int.Parse(id.Text) + " order by so_size asc");
            drp.DataSource = tb;
            drp.DataTextField = "so_size";
            drp.DataValueField = "id_size";
            drp.DataBind();

            // keep size da chon
             if (Session["size"].ToString() != "")
             {
                for (int i = 0; i <= drp.Items.Count-1; i++)
                 {
                      if (Session["size"].ToString()==drp.Items[i].ToString())
                      {
                          drp.SelectedIndex = i;
                      }
                 }
              }
            // tinh trang
            Label tt = new Label();
            tt = (Label)e.Item.FindControl("lbl_tinh_trang");

            // textbox so luong
            TextBox tx = new TextBox();
            tx = (TextBox)e.Item.FindControl("txt_slg");
            if (Session["slg"].ToString() == "")
            {
                tx.Text = "1";
            }
            else { tx.Text = Session["slg"].ToString(); }

            //enable and disable nut mua ngay
            ImageButton ib = new ImageButton();
            ib = (ImageButton)e.Item.FindControl("b_mua_ngay");
            if (tt.Text == "0") // 0 ko, 1 ko , 2 sap ve 
            {
                tt.Text = " Hết hàng";
                ib.Visible = false;
                tx.Enabled = false;
               // tx.Visible = false;
               // lbl_img_new.Visible = false;
                
            }
            else if (tt.Text == "1")
            {
                tt.Text = " Còn hàng";
                ib.Visible = true;
            }
            else
            {
                tt.Text = " Sắp về";
                ib.Visible = true;
            }

            //nut thanh toan;
            XL_Gio_hang bang_gio_hang = (XL_Gio_hang)Session["gio_hang"];
            Image tht = new Image();
            tht = (Image)e.Item.FindControl("b_thanh_toan");
            if (bang_gio_hang.Rows.Count != 0)
            {
                tht.Visible = true;
            }

            Label mota = (Label)e.Item.FindControl("lbl_gioithieu");
            if (mota.Text != "")
            {
                mota.Text = "Mô tả:<br/>" + mota.Text;
            }
            
           

           
        }
        protected void load_hinh_anh(int id)
        {
            DataTable tb = new DataTable();
            tb = XLDL.XLDaTa("select * from V_Hinh_Anh where id_sp=" +id + "");
            StringBuilder str = new StringBuilder();
            string path = System.Configuration.ConfigurationManager.AppSettings["Images_Path"];
            if (tb.Rows.Count != 0)
            {
                str.Append("<table border='0' width='100%'>");
                for (int i = 0; i <= tb.Rows.Count - 1; i++)
                {
                    str.Append("<tr><td align='center'>");
                    str.Append("<img src='" + path + tb.Rows[i]["hinh_anh_2"].ToString() + "' border='0'/>");
                    str.Append("</td></tr>");
                }
                str.Append("</table>");
                lbl_img.Text = str.ToString();
            }
        }
        protected void DataList_load_ItemCommand(object source, DataListCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "mua_ngay")
                {

                    XL_Gio_hang bang_gio_hang = (XL_Gio_hang)Session["gio_hang"];
                    int ms = int.Parse(e.CommandArgument.ToString());
                    Label gia_sp = (Label)e.Item.FindControl("lbl_Gia_SP");
                    Label idnhom = (Label)e.Item.FindControl("lbl_id_nhom");
                    HyperLink ten_sp = (HyperLink)e.Item.FindControl("HyperLink_tensp");
                    DropDownList so_size = (DropDownList)e.Item.FindControl("drp_so_size");
                    //  so luong
                    TextBox slg = new TextBox();
                    slg = (TextBox)e.Item.FindControl("txt_slg");
                    //if (Session["slg"].ToString() != "")
                    //{
                    //    //TextBox slg = new TextBox();
                    //    //slg = (TextBox)DataList_load.Items[0].FindControl("txt_slg");
                    //    slg.Text = Session["slg"].ToString();
                    //}
                    DataTable tb = new DataTable();
                    tb = XLDL.XLDaTa("select * from V_SanPham where id_sp=" + ms + "");
                    int d = bang_gio_hang.Rows.Count;
                    bool ok = false;

                    if (d == 0)
                    {
                        if (int.Parse(slg.Text) > 0)
                        {
                            DataRow dong_moi = bang_gio_hang.NewRow();
                            dong_moi["STT"] = 1;
                            dong_moi["Id_SP"] = ms;
                            dong_moi["Id_nhom_SP"] = int.Parse(idnhom.Text);
                            dong_moi["ten_SP"] = ten_sp.Text;
                            dong_moi["so_luong"] = int.Parse(slg.Text);
                            dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                            dong_moi["so_size"] = so_size.SelectedItem.ToString();
                            dong_moi["id_gt"] = tb.Rows[0]["id_gt"].ToString();
                            dong_moi["gioi_tinh_SP"] =tb.Rows[0]["gioi_tinh_sp"].ToString();
                            dong_moi["hinh_anh"] = tb.Rows[0]["hinh_anh"].ToString();
                            bang_gio_hang.Rows.Add(dong_moi);
                        }
                      //  else { return; }
                    }
                    else
                    {
                        if (int.Parse(slg.Text) > 0)
                        {
                            for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                            {
                                if (bang_gio_hang.Rows[i]["so_size"].ToString() == so_size.SelectedItem.ToString() && bang_gio_hang.Rows[i]["id_sp"].ToString() == ms.ToString())
                                {
                                    int sl = (int)bang_gio_hang.Rows[i]["so_luong"];
                                    bang_gio_hang.Rows[i]["so_luong"] = sl + int.Parse(slg.Text);
                                    ok = true;
                                    break;
                                }
                            }
                            if (ok != true)
                            {
                                DataRow dong_moi = bang_gio_hang.NewRow();
                                dong_moi["STT"] = 1;
                                dong_moi["Id_SP"] = ms;
                                dong_moi["Id_nhom_SP"] = int.Parse(idnhom.Text);
                                dong_moi["ten_SP"] = ten_sp.Text;
                                dong_moi["so_luong"] = int.Parse(slg.Text);
                                dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
                                dong_moi["so_size"] = so_size.SelectedItem.ToString();
                                dong_moi["gioi_tinh_SP"] = tb.Rows[0]["gioi_tinh_sp"].ToString();
                                dong_moi["id_gt"] = tb.Rows[0]["id_gt"].ToString();
                                dong_moi["hinh_anh"] = tb.Rows[0]["hinh_anh"].ToString();
                                bang_gio_hang.Rows.Add(dong_moi);
                            }

                            int stt = 0;
                            for (int i = 0; i <= bang_gio_hang.Rows.Count - 1; i++)
                            {
                                stt += 1;
                                bang_gio_hang.Rows[i]["STT"] = stt;
                            }
                            // cap nhat sTT

                        }
                        
                    }
                    Session["slg"] = slg.Text;
                    Session["size"] = so_size.SelectedItem.ToString();
                  //  Session["slg"] = slg.Text;
                    // cap nhat tat ca vao bang
                    bang_gio_hang.AcceptChanges();
                    Session["gio_hang"] = bang_gio_hang;

                    // dem so lan xem
                   // Counter(ms);


                }
                xuat_luoi();
                ImageButton b = (ImageButton)e.Item.FindControl("b_mua_ngay");
                b.Focus();

            }
            catch { }



            //    XL_Gio_hang bang_gio_hang = (XL_Gio_hang)Session["gio_hang"];
            //     int ms = int.Parse(e.CommandArgument.ToString());
            //     Label gia_sp = (Label)e.Item.FindControl("lbl_Gia_SP");
            //    HyperLink ten_sp = (HyperLink)e.Item.FindControl("HyperLink_tensp");
            //    DropDownList size = (DropDownList)e.Item.FindControl("drp_size_id");
            //    DataRow dong = bang_gio_hang.Rows.Find(ms);

            //       if (dong != null)
            //         {
            //       int sl = (int)dong["so_luong"];
            //       dong["so_luong"] = sl + 1;


            //           }
            //    else
            //    {
            //        try
            //        {
            //            //  bang_gio_hang = new XL_Gio_hang();
            //            DataRow dong_moi = bang_gio_hang.NewRow();
            //            dong_moi["Id_SP"] = ms;
            //            dong_moi["ten_SP"] = ten_sp.Text; 
            //            dong_moi["so_luong"] = 1;
            //            dong_moi["gia_sp"] = double.Parse(gia_sp.Text);
            //            dong_moi["size"] = size.SelectedItem.ToString();
            //            bang_gio_hang.Rows.Add(dong_moi);
            //        }
            //        catch { }
            //    }
            //    bang_gio_hang.AcceptChanges();
            //    Session["gio_hang"] = bang_gio_hang;


            //}
            //xuat_luoi();
        }

        protected void DataList_load_ItemCreated(object sender, DataListItemEventArgs e)
        {
            // luot xem

            DataTable tb = new DataTable();
            Label luotxem = (Label)e.Item.FindControl("lbl_luotxem");
            string id = Request.QueryString["id"].ToString();
            Counter(int.Parse(id));
            tb = XLDL.XLDaTa("select * from V_Counter where id_sp=" + int.Parse(id) + "");
            luotxem.Text = " <span class='productSpecialPrice'><i>Lượt xem: " + tb.Rows[0]["solanxem_sp"].ToString() + "</i></span>";
        }

       

       
       

        
    }
}